<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="登记人">
        <el-input v-model="formInline.name" placeholder="登记人"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="handleInsert">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="responseResult.products" style="width: 100%">
      <el-table-column label="主键" width="80">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借款信息" width="180">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>信用代码: 91110000100010433L</p>
            <p>电话: 18659990088</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.borrowInfo }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="登记人" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.registrant }}</span>
        </template>
      </el-table-column>

      <el-table-column label="保全时间" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.applicationTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="保全内容" width="80">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.guaranty }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="附件" width="80">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.appendix }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleUpdate(scope.$index, scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <span class="demonstration">完整功能</span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="responseResult.pageNumber"
        :page-sizes="[5, 5, 5, 5]"
        :page-size="responseResult.page_SIZE"
        layout="total, sizes, prev, pager, next, jumper"
        :total="responseResult.total"
      >
      </el-pagination>
    </div>
    <!-- 新增表单dialog -->
    <el-dialog title="资产保全" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="借款信息" :label-width="formLabelWidth">
          <el-input v-model="form.borrowInfo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="登记人" :label-width="formLabelWidth">
          <el-input v-model="form.registrant" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="担保物" :label-width="formLabelWidth">
          <el-select v-model="form.guaranty" placeholder="请选择抵押物">
            <el-option label="房子" value="1"></el-option>
            <el-option label="车子" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附件" :label-width="formLabelWidth">
          <el-input v-model="form.appendix" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submintInsert">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改表单 -->
    <el-dialog title="修改" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="借款信息" :label-width="formLabelWidth">
          <el-input v-model="form.borrowInfo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="登记人" :label-width="formLabelWidth">
          <el-input v-model="form.registrant" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="担保物" :label-width="formLabelWidth">
          <el-select v-model="form.guaranty" placeholder="请选择抵押物">
            <el-option label="房子" value="1"></el-option>
            <el-option label="车子" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附件" :label-width="formLabelWidth">
          <el-input v-model="form.appendix" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 表格dialog -->
    <el-dialog
      title="详情"
      :visible.sync="dialogTableVisible"
      style="width=1000px"
    >
      <el-table :data="dialogResponseResult.products">
        <el-table-column property="id" label="主键"></el-table-column>
        <el-table-column
          property="repaymentDate"
          label="应该还款日期"
        ></el-table-column>
        <el-table-column
          property="repaymentPeriod"
          label="周期"
        ></el-table-column>
        <el-table-column
          property="principalRepayable"
          label="应还本金"
          width="150"
        ></el-table-column>
        <el-table-column
          property="interestRepayable"
          label="应还利息"
        ></el-table-column>
        <el-table-column
          property="totalRepayment"
          label="总共应还金额"
        ></el-table-column>
        <el-table-column
          property="actualRepaymentAmount"
          label="实际还款金额"
        ></el-table-column>
        <el-table-column
          property="actualRepaymentDate"
          label="实际还款日期"
        ></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              v-if="scope.row.actualRepaymentAmount != scope.row.totalRepayment"
              @click="handleRepayment(scope.$index, scope.row)"
              >立即还款</el-button
            >
            <el-button
              size="mini"
              class="del-com"
              type="primary"
              v-if="scope.row.actualRepaymentAmount == scope.row.totalRepayment"
              >本期已还</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <span class="demonstration">完整功能</span>
        <el-pagination
          @current-change="dialogHandleCurrentChange"
          :current-page="dialogResponseResult.pageNumber"
          :page-sizes="[5, 5, 5, 5]"
          :page-size="dialogResponseResult.page_SIZE"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dialogResponseResult.total"
        >
        </el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // <<< 查询条件 <<<
      formInline: {
        name: "",
        status: "",
      },
      // >>> 查询条件 >>>

      // <<< 响应结果 后端的pagebean <<
      responseResult: {
        endIndex: 3,
        pageNumber: 1,
        page_SIZE: 5,
        products: [
          {
            id: 1,
            borrowInfo: 2,
            registrant: "lili",
            applicationTime: "2021-11-12T06:45:54.000+00:00",
            guaranty: 1,
            appendix: "appendix",
          },
          {
            id: 2,
            borrowInfo: 1,
            registrant: "张三",
            applicationTime: "2021-11-12T06:44:37.000+00:00",
            guaranty: 1,
            appendix: "1234",
          },
        ],
        startIndex: 1,
        total: 14,
        totalPage: 3,
      },
      // >>> 响应结果 >>>

      // <<< dialog <<<
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        borrowInfo: "",
        registrant: "",
        guaranty: "",
        appendix: "",
      },
      formLabelWidth: "120px",
      manageId: 1,
      // <<< 响应结果 后端的pagebean <<
      dialogResponseResult: {
        endIndex: 3,
        pageNumber: 1,
        page_SIZE: 5,
        products: [
          {
            id: 1,
            manageInfo: 1,
            repaymentDate: null,
            repaymentPeriod: 1,
            principalRepayable: 1000,
            interestRepayable: 10,
            totalRepayment: 1010,
            actualRepaymentAmount: 1000,
            actualRepaymentDate: "2021-11-16T07:06:43.000+00:00",
          },
          {
            id: 2,
            manageInfo: 1,
            repaymentDate: "3921-11-19T18:00:00.000+00:00",
            repaymentPeriod: 2,
            principalRepayable: 1000,
            interestRepayable: 10,
            totalRepayment: 1010,
            actualRepaymentAmount: 1000,
            actualRepaymentDate: "2021-11-16T07:08:07.000+00:00",
          },
        ],
        startIndex: 1,
        total: 14,
        totalPage: 3,
      },
      // >>> 响应结果 >>>
      // >>> dialog  >>>
    };
  },
  created() {
    this.onSubmit();
  },
  methods: {
    onSubmit() {
      // 先把分页重置

      let url = "http://localhost:9823/protection/selectall/";
      // url 拼接
      url = url + "1" + "?" + "registrant=" + this.formInline.name; // 后期做 企业名称
      //console.log(url);
      this.$axios.get(url).then((resp) => {
        // 得到后端传的pageBean
        this.responseResult = resp.data.data;
        console.log(this.responseResult);
      });
      //console.log("submit!");
    },
    handleInsert() {
      this.dialogFormVisible = true;
    },
    submintInsert() {
      this.dialogFormVisible = false;
      let url = "http://localhost:9823/protection/insert";
      // url 拼接
      let data = this.form;
      this.$axios.post(url, data).then((resp) => {
        // 得到后端传的pageBean
        console.log(this.responseResult);
      });
      this.onSubmit();
    },
    handleEdit(index, row) {
      this.dialogTableVisible = true;
      // console.log(index);
      // console.log(row);
      let url = "http://localhost:9823/registration/selectall/1?manageInfo=";
      // url 拼接
      url = url + row.id;
      this.manageId = row.id;
      //console.log(url);
      this.$axios.get(url).then((resp) => {
        // 得到后端传的pageBean
        this.dialogResponseResult = resp.data.data;
        // console.log(this.dialogResponseResult);
      });
    },
    handleUpdate(index, row) {
      this.dialogFormVisible = true;
      this.form = row;
    },
    updateSubmit() {
      this.dialogFormVisible = false;
      let url = "http://localhost:9823/protection/update";
      // url 拼接
      let data = this.form;
      this.$axios.put(url, data).then((resp) => {
        // 得到后端传的pageBean
        console.log(this.responseResult);
      });
      this.onSubmit();
    },
    // <<< dialog <<<
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      let url = "http://localhost:9823/protection/selectall/";
      // url 拼接
      url = url + val + "?" + "registrant=" + this.formInline.name; // 后期做 企业名称
      //console.log(url);
      this.$axios.get(url).then((resp) => {
        // 得到后端传的pageBean
        this.responseResult = resp.data.data;
        console.log(this.responseResult);
      });
    },
    dialogHandleCurrentChange(val) {
      let url =
        "http://localhost:9823/registration/selectall/" + val + "?manageInfo=";
      // url 拼接
      url = url + this.manageId;
      //console.log(url);
      this.$axios.get(url).then((resp) => {
        // 得到后端传的pageBean
        this.dialogResponseResult = resp.data.data;
        //console.log(this.dialogResponseResult);
      });
    },
    // >>> dialog >>>
  }
};
</script>
    
<style>
</style>